

package net.jjjshop.front.controller.user;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.lbt.LbUserWalletEntity;
import net.jjjshop.common.entity.order.Order;
import net.jjjshop.common.entity.supplier.Supplier;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.enums.OrderTypeEnum;
import net.jjjshop.common.enums.SettingEnum;
import net.jjjshop.common.service.lbt.LbLineUpService;
import net.jjjshop.common.service.lbt.LbUserWalletService;
import net.jjjshop.common.settings.vo.PointsVo;
import net.jjjshop.common.util.SettingUtils;
import net.jjjshop.common.util.poster.ExtractUtils;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.core.pagination.Paging;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.framework.util.PasswordUtil;
import net.jjjshop.front.controller.BaseController;
import net.jjjshop.front.param.order.OrderPageParam;
import net.jjjshop.front.param.order.OrderPayParam;
import net.jjjshop.front.service.app.AppService;
import net.jjjshop.front.service.order.OrderService;
import net.jjjshop.front.service.supplier.SupplierService;
import net.jjjshop.front.service.user.UserOrderService;
import net.jjjshop.front.vo.order.OrderDetailVo;
import net.jjjshop.front.vo.order.OrderListVo;
import net.jjjshop.front.vo.settings.ExpressDetailVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(value = "user", tags = {"用户订单管理"})
@RestController
@RequestMapping("/front/user/order")
public class UserOrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AppService appService;
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private ExtractUtils extractUtils;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private LbUserWalletService lbUserWalletService;
    @Autowired
    private LbLineUpService lineUpService;
    @Autowired
    private SupplierService supplierService;

    @RequestMapping(value = "/lists", method = RequestMethod.POST)
    @OperationLog(name = "lists")
    @ApiOperation(value = "lists", response = String.class)
    public ApiResult<Paging<OrderListVo>> lists(@RequestBody OrderPageParam orderPageParam){
        orderPageParam.setUserId(this.getUser(true).getUserId());
        return ApiResult.ok(orderService.getList(orderPageParam));
    }

    @RequestMapping(value = "/toPay", method = RequestMethod.GET)
    @OperationLog(name = "toPay")
    @ApiOperation(value = "toPay", response = String.class)
    public ApiResult<Map<String, Object>> toPay(String orderId, String paySource){
        User user = this.getUser(true);
        Map<String, Object> result = new HashMap<>();
        result.put("payPrice", orderService.getTotalPayMoney(orderId, user.getUserId()));
        LbUserWalletEntity userWalletInfo = lbUserWalletService.getUserWalletInfo(user.getUserId());
        OrderDetailVo detail = orderService.detail(Integer.parseInt(orderId));
        List<Integer> payType = new ArrayList<>();
        boolean isGpay = false;
        if (null != detail){
            Supplier byId = supplierService.getById(detail.getShopSupplierId());
            if (null != byId){
                if (byId.getShopType() ==1){
                    isGpay = true;
                }else if (byId.getShopType() == 0){
                    payType.add(10);
                    //这是是第1个模块商家
//                    Integer userOnePay = orderService.getUserOnePay(user.getUserId());
//                    if (userOnePay >= 1 ){
//                       new Exception("该区域只能下一个订单");
//                    }
                }else if (byId.getShopType() == 2){
                    payType.add(10);
                    //这是是第三个模块商家
//                    Integer userOnePay = orderService.getUserOnePay(user.getUserId());
//                    if (userOnePay <= 0){
//                       new Exception("请先到第一个区域下单");
//                    }
                }
            }
        }


        if (null != userWalletInfo){
            result.put("balance", userWalletInfo.getMoney());
            result.put("integral", userWalletInfo.getIntegral());
            if (isGpay){
//                result.put("integral", userWalletInfo.getIntegral());
                payType.add(40);
            }
        }else{
            result.put("balance", user.getBalance());
        }

        // 开启的支付类型
        result.put("payTypes", payType);
        return ApiResult.ok(result);
    }

    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    @OperationLog(name = "pay")
    @ApiOperation(value = "pay", response = String.class)
    public ApiResult<?> pay(@RequestBody OrderPayParam orderPayParam){
        User user = this.getUser(true);
        if (StringUtils.isEmpty(orderPayParam.getPayPass())){
            return  ApiResult.fail("交易密码不能为空");
        }
        if (StringUtils.isEmpty(user.getPayPass())){
            return  ApiResult.fail("请先设置交易密码");
        }
        String md5Hex = DigestUtils.md5Hex(orderPayParam.getPayPass());
        if (!md5Hex.equals(user.getPayPass())){
            return ApiResult.fail("交易密码不正确");
        }
//        Integer num =  lineUpService.gpNum(user.getUserId(),orderService.getById(Integer.parseInt(orderPayParam.getOrderId())).getShopSupplierId());
//        if (null == num || num.intValue() == 0) {
//
//        }else{
//            return ApiResult.fail("您订单还处于公排中");
//        }
        Map<String, Object> result = orderService.orderPay(orderPayParam, user);
        // 订单类型
        result.put("orderType", OrderTypeEnum.MASTER.getValue());
        return ApiResult.ok(result);
    }

    @RequestMapping(value = "/paySuccessDetail", method = RequestMethod.POST)
    @OperationLog(name = "paySuccessDetail")
    @ApiOperation(value = "paySuccessDetail", response = String.class)
    public ApiResult<Map<String, Object>> paySuccessDetail(String orderId){
        Map<String, Object> result = new HashMap<>();
        User user = this.getUser(true);
        String[] orderIds = orderId.split(",");
        BigDecimal payPrice = BigDecimal.ZERO;
        int pointsBonus = 0;
        for(String id:orderIds){
            OrderDetailVo order = userOrderService.detail(user, Integer.parseInt(id));
            payPrice = payPrice.add(order.getPayPrice());
            pointsBonus += order.getPointsBonus();
        }
        // 支付金额
        result.put("payPrice", payPrice);
        // 赠送积分
        result.put("pointsBonus", pointsBonus);
        return ApiResult.ok(result);
    }

    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @OperationLog(name = "cancel")
    @ApiOperation(value = "cancel", response = String.class)
    public ApiResult<String> cancel(Integer orderId){
        User user = this.getUser(true);
        Order order = orderService.getUserOrderDetail(orderId, user.getUserId());
        if(orderService.cancelOrder(order)){
            return ApiResult.ok(null, "取消成功");
        }else{
            return ApiResult.fail("取消失败");
        }
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @OperationLog(name = "detail")
    @ApiOperation(value = "detail", response = String.class)
    public ApiResult<Map<String, Object>> detail(Integer orderId){
        Map<String, Object> result = new HashMap<>();
        result.put("detail", userOrderService.detail(this.getUser(true),orderId));
        JSONObject points = settingUtils.getSetting(SettingEnum.POINTS.getKey(), null);
        PointsVo pointsVo = points.toJavaObject(PointsVo.class);
        result.put("pointsName", pointsVo.getPointsName());
        //todo 店铺客服信息
        result.put("mpService", "");
        //todo 是否开启客服
        result.put("serviceOpen", false);
        return ApiResult.ok(result);
    }

    @RequestMapping(value = "/express", method = RequestMethod.GET)
    @OperationLog(name = "express")
    @ApiOperation(value = "express", response = String.class)
    public ApiResult<ExpressDetailVo> express(Integer orderId)  throws Exception  {
        return ApiResult.ok(userOrderService.express(orderId));
    }

    @RequestMapping(value = "/receipt", method = RequestMethod.POST)
    @OperationLog(name = "receipt")
    @ApiOperation(value = "receipt", response = String.class)
    public ApiResult<String> receipt(Integer orderId){
        if(orderService.receipt(orderId)){
            return ApiResult.ok(null, "收货成功");
        }else{
            return ApiResult.fail("收货失败");
        }
    }

    @RequestMapping(value = "/qrcode", method = RequestMethod.GET)
    @OperationLog(name = "qrcode")
    @ApiOperation(value = "qrcode", response = String.class)
    public ApiResult<String> qrcode(Integer orderId){
        User user = this.getUser(true);
        return ApiResult.ok(extractUtils.genePoster(orderId, user));
    }



}
